package com.baizhi.controller;


import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;

import javax.security.auth.Subject;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("login")
    @ResponseBody
    public Map login(Admin admin, String code, HttpSession session){
        Map map = new HashMap();
        String  vc = (String)session.getAttribute("vc");
        if(!vc.equalsIgnoreCase(code)){
            map.put("flag",false);
            map.put("msg","验证码输入错误!!!");
            return map;
        }
        //封装令牌
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(),admin.getPassword());
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        //认证
        try {
            subject.login(token);
            session.setAttribute("name",admin.getUsername());
            if(subject.hasRole("admin")){
                System.out.println("登陆成功,当前用户具有admin的角色");
            }
            if(subject.hasRole("superadmin")){
                System.out.println("登陆成功,当前用户具有superadmin的角色");
            }
            if(subject.isPermitted("banner:create")){
                System.out.println("具有添加轮播图的权限");
            }
            map.put("flag",true);
        } catch (Exception e) {
            System.out.println("打印语句验证失败");
            map.put("flag",false);
            map.put("msg","验证失败");
        }

        return map;
    }
    @RequestMapping("selectPage")
    @ResponseBody
    public Map selectPage(Integer page,Integer limit){
        Map m = new HashMap();
        Subject subject = SecurityUtils.getSubject();
        if(subject.hasRole("superadmin")){
            PageInfo<Map> pageInfo = adminService.selectAll(page, limit);
            m.put("code",0);
            m.put("msg","");
            m.put("count",pageInfo.getTotal());
            m.put("data",pageInfo.getList());
            return m;
        }
        return null;

    }
    @RequestMapping("add")
    @ResponseBody
    public Map add(Admin admin,Integer role_id){
        Map m = new HashMap();
        try {
            adminService.insert(admin);
            adminService.add(admin.getId(),role_id);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @RequestMapping("delete")
    @ResponseBody
    public Map delete(Integer id){
        Map m = new HashMap();
        try {
            adminService.deleteByPrimaryKey(id);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @RequestMapping("selectOne")
    @ResponseBody
    public Admin selectOne(Integer id){
        Admin admin = adminService.selectByPrimaryKey(id);
        return admin;
    }
    @RequestMapping("update")
    @ResponseBody
    public Map update(Admin admin){
        Map m = new HashMap();
        try {
            adminService.updateByPrimaryKeySelective(admin);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
}
